package com.snowstore.mars.service.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.springframework.stereotype.Service;

import com.snowstore.mars.utilities.common.ProgressBarHelp;
/**
 * 
 * @description:进度条过滤器
 *
 * @author lilerong
 *
 * @version 1.0 ,2015年11月26日 下午5:22:07
 *
 */
@Service
public class ProgressBarFilter implements Filter {

	public void init(FilterConfig filterConfig) throws ServletException {
	
	}
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
		response.setContentType("text/html;charset=UTF-8");
		//获取进度条js中的参数：progressKey对应的是方法名，currentValue：对应的是进度。
		String progressKey=request.getParameter("progressKey");
		String currentValue=request.getParameter("currentValue");
		String valueNew=ProgressBarHelp.getProgressValue(progressKey);//
		//1:设置值为0
		if(valueNew==null){
			valueNew="0";
		}
		if(currentValue==null){
			currentValue="0";
		}
		//2：做移除，要不然会出现重复任务
		if("100".equals(valueNew)||Integer.valueOf(valueNew)>100){
			ProgressBarHelp.removeProgress(progressKey); 
		}
		//3:比较是为了防止多浏览器的问题。
		if(Integer.valueOf(valueNew)<Integer.valueOf(currentValue)){
			valueNew="100";
		}
		//4:把结果写出去
		response.getWriter().write(valueNew);
	}
	public void destroy() {
	
	}
 
}
